package sz.szsmk.citizencard.mvp.presenter;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.model.network.bean.request.AccumPointReq;
import sz.szsmk.citizencard.model.network.bean.request.SignInReq;
import sz.szsmk.citizencard.model.network.bean.request.UserBehaviorBrowseReq;
import sz.szsmk.citizencard.model.network.bean.response.AccumPointRes;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.SignInRes;
import sz.szsmk.citizencard.mvp.ISignInMvpView;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.AppBasePresenter;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;

/**
 * author : hyt
 * date : 2019/8/23
 * description :
 */
public class SignInPresenter extends AppBasePresenter<ISignInMvpView> {

    private NetworkDateSources mNetworkDateSource;

    @Inject
    public SignInPresenter() {
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


//    查询是否签到
    public void checkSign(){
        SignInReq signInReq = new SignInReq();
        signInReq.setAccess_token(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));

        //签到
        mNetworkDateSource.checkSign(signInReq, new SubscribeResult<SignInRes>() {
            @Override
            protected void onOk(SignInRes response) {
                getMvpView().checkSignResult(response);
            }

            @Override
            protected void onFailure(SignInRes response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    //签到
    public void signIn(){
        SignInReq signInReq = new SignInReq();
        signInReq.setEventOrigin(13);       //固定
        signInReq.setAccess_token(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));
        signInReq.setUserId(ACache.get(DaggerApplication.getIns()).getAsString("userId").toString());

        //签到
        mNetworkDateSource.signIn(signInReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                getMvpView().onSignInSuccess();
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                getMvpView().onFailure(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    //获取积分
    public void getScore(){
        SignInReq signInReq = new SignInReq();
        signInReq.setAccess_token(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));

        mNetworkDateSource.getUserScore(signInReq, new SubscribeResult<SignInRes>() {
            @Override
            protected void onOk(SignInRes response) {
                getMvpView().getScore(response);
            }

            @Override
            protected void onFailure(SignInRes response) {
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    //积分列表
    public void getScoreList(AccumPointReq accumPointReq){
        accumPointReq.setAccess_token(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));
        accumPointReq.setPage_size(20);
        mNetworkDateSource.getScoreList(accumPointReq, new SubscribeResult<AccumPointRes>() {
            @Override
            protected void onOk(AccumPointRes response) {
                getMvpView().getScoreListResult(response);
            }

            @Override
            protected void onFailure(AccumPointRes response) {
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void userBehaviorBrowse() {
        UserBehaviorBrowseReq userBehaviorBrowseReq = new UserBehaviorBrowseReq();
        userBehaviorBrowseReq.setModularId( 45 );
        userBehaviorBrowseReq.setAccessToken(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));

        mNetworkDateSource.userBehaviorBrowse(userBehaviorBrowseReq, new Action1<BaseBean>() {
            @Override
            public void call(BaseBean baseBean) {
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }





}
